package controller;

import model.Goods;
import model.Type;
import service.GoodsService;
import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "Index", value = "/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TypeService ts = new TypeService();
        GoodsService gs = new GoodsService();
        List<Type> typeList;
        try {
            //获取商品的系列
            typeList = ts.getTypeList();
            req.getSession().setAttribute("typeList", typeList);
            //获取轮播的商品
            List<Goods> scroll = gs.getScroll();
            req.getSession().setAttribute("scroll", scroll);
            //获取热销的商品
            List<Goods> hotlist = gs.getHost(2);
            req.getSession().setAttribute("hotList", hotlist);
            //获取新品商品
            List<Goods> recoment = gs.getHost(3);
            req.getSession().setAttribute("newList", recoment);

            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } catch (SQLException throwables) {
            req.getRequestDispatcher("index.jsp").forward(req, resp);
            throwables.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
